// MCE status for LCDSmartie
// Based on DemoC++Plugin.cpp : Defines the entry points for the DLL application.
//

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include "MCE_dll_private.h"

#define DLLEXPORT __declspec(dllexport)

extern "C" int    __stdcall MCE_dll_start(); 
extern "C" void   __stdcall MCE_read_ini_file();
extern "C" char * __stdcall MCE_dll_getvals1();
extern "C" char * __stdcall MCE_dll_getvals2();
extern "C" char * __stdcall MCE_dll_getvals11();
extern "C" char * __stdcall MCE_dll_getvals12();
extern "C" char * __stdcall MCE_dll_get_tag_value(char *);
extern "C" char * __stdcall MCE_dll_get_tag_values(char *, char *);
extern "C" int    __stdcall Topen_log();
extern "C" int    __stdcall Tclose_log();
extern "C" int    __stdcall Tlog(char *,char *);
extern "C" int    __stdcall MCE_dll_check_process(char *procname);
extern "C" void   __stdcall MCE_check_tags(void);
extern "C" void   __stdcall MCE_check_counter();

static int initialised=0;
static char result[256];

/*********************************************************
 *         SmartieInit                                   *
 *********************************************************/
extern "C" DLLEXPORT  void __stdcall  SmartieInit()
{
    // This function will be called when the plugin is 1st loaded.
	// This function is OPTIONAL.
	if (initialised==0)
     { initialised=1;
       MCE_read_ini_file();
       MCE_check_tags();
       MCE_dll_start();
       Tlog("SmartieInit","Started");
     }
    else Tlog("SmartieInit","Second call ignored");
}

/*********************************************************
 *         SmartieFini                                   *
 *********************************************************/
extern "C" DLLEXPORT  void __stdcall  SmartieFini()
{
	// This function will be called just before the plugin
	// is unloaded. This function is OPTIONAL.
	Tlog("SmartieFini","Shutting Down");
    Tclose_log();
}

/*********************************************************
 *         GetMinRefreshInterval                         *
 *********************************************************/
extern "C" DLLEXPORT  int __stdcall  GetMinRefreshInterval()
{
	//
	// Define the minimum interval that a screen should get fresh
	// data from our plugin.
	// The actual value used by Smartie will be the higher of this
	// value and of the "dll check interval" setting
	// on the Misc tab.  [This function is optional, Smartie will
	// assume 300ms if it is not provided.]
	// 
	return 300; // 300 ms
}


/*********************************************************
 *         Function 1                                    *
 *  Returns the first display line       			     *
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function1(char *param1, char *param2)
{
    MCE_check_counter();
    return MCE_dll_getvals1();   
}


/*********************************************************
 *         Function 2                                    *
 *  Returns the second display line                      *
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function2(char *param1, char *param2)
{
    MCE_check_counter();
    return MCE_dll_getvals2();  
}



/*********************************************************
 *         Function 3                                    *
 *  Returns the version number of my code                *
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function3(char *param1, char *param2)
{ 
    MCE_check_counter();
    sprintf(result,"%s %s","MCE status plugin v",VER_STRING);
    return result;
}
    


/************************************************************
 *         Function 4                                       *
 *  Returns the value of the tag(s) supplied as a parameter *
 *  If 2 tags are supplied then you will get string/string  *
 ***********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function4(char *param1, char *param2)
{    
    MCE_check_counter();
    return MCE_dll_get_tag_values(param1,param2);   
}



int substrcpy(unsigned char *out, unsigned char *in, unsigned char c)
{ int i,ret;

  ret=0;
  for (i=0;i<(strlen((char *)in)+1);i++)
    { out[i]=in[i]; if ((in[i]==c) || (in[i]==0)) { out[i]=0; ret=i; break; } }
  return ret;
}
 
/*********************************************************
 *         Function 9                                    *
 *  Performs an if function e.g. $dll(mce_dll,9,a#b,c#d) *
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function9(char *param1, char *param2)
{ unsigned char str1[80],str2[80];
  static unsigned char str3[80],str4[80];
  int i,j;
  
  i=substrcpy(str1,(unsigned char *)param1,'#');       if(i==0) return "Param1 Missing";
  j=substrcpy(str2,(unsigned char *)&param1[i+1],'#'); if(j==0) return "Param2 Missing";
  i=substrcpy(str3,(unsigned char *)param2,'#');       if(i==0) return "Param3 Missing";
  j=substrcpy(str4,(unsigned char *)&param2[i+1],'#'); if(j==0) return "Param4 Missing"; 
  
  if(!strcmp((char *)str1,(char *)str2)) return (char *)str3;
  return (char *)str4;
}



/*********************************************************
 *         Function 11                                    *
 *  Returns the first display line using session control  *
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function11(char *param1, char *param2)
{
    MCE_check_counter();
    return MCE_dll_getvals11();   
}


/*********************************************************
 *         Function 12                                    *
 *  Returns the second display line using session control *
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function12(char *param1, char *param2)
{
    MCE_check_counter();
    return MCE_dll_getvals12();  
}



/*********************************************************
 *         Function 19                                    *
 *  ReReads the ini file and reopens the log file         *
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function19(char *param1, char *param2)
{ 
    MCE_read_ini_file();
//    Topen_log();
    MCE_check_counter();
    return "OK";    
}



/*********************************************************
 *         Function 20                                    *
 *  Check if the given process is active (x.exe)          *
 * Returns param2 if it is, The value of the tag          *
 * ProcessInactive if not                                 *
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function20(char *param1, char *param2)
{
    MCE_check_counter();
    int i = MCE_dll_check_process(param1);  
//    if (i==1) return param2; else return "0";    
    if (i==1) return param2; else return MCE_dll_get_tag_value("ProcessInactive");
}

